package com.ruoyi.StarStudio.Controller;

import com.github.pagehelper.PageInfo;
import com.ruoyi.StarStudio.Utils.QueryPageUtils;
import com.ruoyi.StarStudio.Domain.*;
import com.ruoyi.StarStudio.Service.AnnualOutputService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/output")
public class AnnualOutputController {

    @Autowired
    private AnnualOutputService annualoutputService;

    //根据地块id查询年产量信息
    @GetMapping("/{zone_id}")
    public Result getByZoneId(@PathVariable Integer zone_id){
        List<Annualoutput> annualOutputList = null;
        try {
            annualOutputList = annualoutputService.getByZoneId(zone_id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.ANNUAL_OUTPUT_QUERY_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.ANNUAL_OUTPUT_QUERY_SUCCESS, annualOutputList);
    }

    /**
     * 导出地块年产量信息到 Excel表格
     * @param response
     * @param annualoutput
     */
    @Log(title = "地块管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Annualoutput annualoutput) {
        List<Annualoutput> list = annualoutputService.getList(annualoutput);
        ExcelUtil<Annualoutput> util = new ExcelUtil<Annualoutput>(Annualoutput.class);
        util.exportExcel(response, list, "地块年产量数据");
    }

    /**
     * 从Excel表格导入地块数据
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "年产量管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<Annualoutput> util = new ExcelUtil<Annualoutput>(Annualoutput.class);
        List<Annualoutput> annualoutputList = util.importExcel(file.getInputStream());
        String message = annualoutputService.importAnnualOutput(annualoutputList, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 导入Excel模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Annualoutput> util = new ExcelUtil<Annualoutput>(Annualoutput.class);
        util.importTemplateExcel(response, "地块年产量数据");
    }

    //根据年产量id查询年产量信息
    @GetMapping("/byId/{annualoutput_Id}")
    public Result getByOutputId(@PathVariable Integer annualoutput_Id){
        Annualoutput byOutputId = null;
        try {
            byOutputId = annualoutputService.getByOutputId(annualoutput_Id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.ANNUAL_OUTPUT_QUERY_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.ANNUAL_OUTPUT_QUERY_SUCCESS, byOutputId);
    }

    //分页搜索查询年产量信息
    @GetMapping("/page")
    public PageResult getList(Annualoutput annualoutput){
        QueryPageUtils.startPage();
        List<Annualoutput> annualOutputList = null;
        PageInfo pageInfo;
        try {
            annualOutputList = annualoutputService.getList(annualoutput);
            pageInfo = new PageInfo<>(annualOutputList);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(HttpStatusCode.ERROR, MessageConstant.ANNUAL_OUTPUT_QUERY_FAIL);
        }
        return new PageResult(HttpStatusCode.SUCCESS, pageInfo.getTotal(), MessageConstant.ANNUAL_OUTPUT_QUERY_SUCCESS, annualOutputList);
    }

    //根据地块id增加年产量信息及中间表数据
    @PostMapping("/{zone_id}")
    public Result add(@RequestBody Annualoutput annualoutput, @PathVariable Integer zone_id) {
        try {
            annualoutput.setZone_id(zone_id);
            annualoutputService.add(annualoutput,zone_id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.ANNUAL_OUTPUT_ADD_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.ANNUAL_OUTPUT_ADD_SUCCESS);
    }

    //修改年产量信息
    @PutMapping
    public Result update(@RequestBody Annualoutput annualoutput){
        try {
            annualoutputService.update(annualoutput);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.ANNUAL_OUTPUT_UPDATE_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.ANNUAL_OUTPUT_UPDATE_SUCCESS);
    }

    //根据年产量id删除年产量信息及中间表数据
    @DeleteMapping("/{annualoutput_Id}")
    public Result delete(@PathVariable Integer annualoutput_Id){
        try {
            annualoutputService.delete(annualoutput_Id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.ANNUAL_OUTPUT_DELETE_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.ANNUAL_OUTPUT_DELETE_SUCCESS);
    }
}
